import UserAPI from "@/api/system/user-api";
import { useTableData } from "@/components/SmTable/src/utils";
import { mockFieldMap, mockList } from "@/utils/mock";
import { mockArr } from "./mock";

export const useTable = () => {
  const list: any = ref([]);
  const total = ref(0);
  const loading = ref(false);

  const { tableOptions, transformTableOptions } = useTableData();

  // const fieldMap = {
  //   username: "登录用户名",
  //   mobile: "手机号",

  //   // institutionName: "归属机构", organization对象
  //   /// deptName: "归属部门",
  //   roles: {
  //     label: "角色",
  //     slot: "roles",
  //   },
  //   createdAt: "创建时间",
  //   updatedAt: "更新时间",
  //   operate: {
  //     label: "操作",
  //     slot: "operate",
  //     fixed: "right",
  //     width: "180px",
  //   },
  // };

  const fieldMap = mockFieldMap(mockArr);

  tableOptions.value = transformTableOptions(fieldMap);

  // 先模拟数据
  const getList = async () => {
    const data = mockList(mockArr);
    list.value = data.list;
    total.value = data.total;
  };

  const queryParams: any = reactive({
    pageNum: 1,
    pageSize: 10,
  });

  /** 查询列表 */
  // const getList = async () => {
  //   loading.value = true;
  //   try {
  //     const data = await UserAPI.getPage(queryParams);
  //     list.value = data.list;
  //     console.log(data, "data-data");
  //     total.value = data.total;
  //   } finally {
  //     loading.value = false;
  //   }
  // };

  function handleQuery() {
    queryParams.pageNum = 1;
    getList();
  }

  const queryFormRef = ref();
  /** 添加/修改操作 */
  const formRef = ref();
  const openForm = (type: string, id?: number) => {
    formRef.value.open(type, id);
  };

  /** 重置按钮操作 */
  const handleResetQuery = () => {
    queryFormRef.value.resetFields();
    nextTick(() => {
      handleQuery();
    });
  };

  return {
    tableOptions,
    list,
    total,
    loading,
    queryParams,
    getList,
    handleQuery,
    handleResetQuery,
    queryFormRef,
    formRef,
    openForm,
  };
};
